
import 'package:amap_flutter_base/amap_flutter_base.dart';

extension NLatLng on LatLng {

  static LatLng? from(var json) {
    if (json is Map) {
      return LatLng(json['latitude'], json['longitude']);
    } else if (json is List) {
      return LatLng(json[0], json[1]);
    }
    return null;
  }
}

class PoiAddress {
  String? neighborhood;
  String? postcode;
  String? building;
  String? province;
  String? level;
  String? formattedAddress;
  LatLng? location;
  String? city;
  String? citycode;
  String? district;
  String? adcode;
  String? township;
  String? country;
  StreetNumber? streetNumber;

  PoiAddress(
      {this.neighborhood,
        this.postcode,
        this.building,
        this.province,
        this.level,
        this.formattedAddress,
        this.location,
        this.city,
        this.citycode,
        this.district,
        this.adcode,
        this.township,
        this.country});

  PoiAddress.fromJson(Map<String, dynamic> json) {
    neighborhood = json['neighborhood'];
    postcode = json['postcode'];
    building = json['building'];
    province = json['province'];
    level = json['level'];
    formattedAddress = json['formattedAddress'];
    location = json['location'] != null
        ? NLatLng.from(json['location'])
        : null;
    city = json['city'];
    citycode = json['citycode'];
    district = json['district'];
    adcode = json['adcode'];
    township = json['township'];
    country = json['country'];
    streetNumber = json['streetNumber'] != null
        ? StreetNumber.fromJson(json['streetNumber'])
        : null;
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['neighborhood'] = neighborhood;
    data['postcode'] = postcode;
    data['building'] = building;
    data['province'] = province;
    data['level'] = level;
    data['formattedAddress'] = formattedAddress;
    if (location != null) {
      data['location'] = location!.toJson();
    }
    data['city'] = city;
    data['citycode'] = citycode;
    data['district'] = district;
    data['adcode'] = adcode;
    data['township'] = township;
    data['country'] = country;
    return data;
  }
}

class StreetNumber {
  String? direction;
  String? number;
  String? street;
  LatLng? location;
  num? distance;

  StreetNumber(
      {this.direction, this.number, this.street, this.location, this.distance});

  StreetNumber.fromJson(Map<String, dynamic> json) {
    direction = json['direction'];
    number = json['number'];
    street = json['street'];
    var j = json['location'];
    if (j != null) {
      location = NLatLng.from(j);
    }
    distance = json['distance'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = <String, dynamic>{};
    data['direction'] = direction;
    data['number'] = number;
    data['street'] = street;
    if (location != null) {
      data['location'] = location!.toJson();
    }
    data['distance'] = distance;
    return data;
  }
}